#!/bin/ash -efu
#
# mki-copy-our2out
#
# This file is part of mkimage
# Copyright (C) 2007-2009  Alexey Gladkov <gladkov.alexey@gmail.com>
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

. "${0%/*}"/mki-sh-functions

cleanup="${CLEANUP_OUTDIR:-}"
subprofile="${SUBPROFILE:-0}"

verbose "has started executing."

if [ "$MKI_OUTNAME" != "${MKI_OUTNAME##*/}" ]; then
	outdir="$(readlink -ev "${MKI_OUTNAME%/*}")"
	cleanup=
fi

[ -d "$chroot" ] ||
	fatal "$dir: not look like a work directory of hasher."

get_copy_args "$outdir" "$chroot/.our"

[ -z "$cleanup" ] || [ "$subprofile" = 0 ] ||
	find "$outdir/" -mindepth 1 -maxdepth 1 -execdir rm $verbose -rf -- '{}' '+'

cd "$chroot/.our"
find -mindepth 1 -maxdepth 1 |
	xargs -r cp $cp_args $verbose -aft "$outdir" --

mki-run /.host/find /.our -mindepth 1 -maxdepth 1 -execdir rm -rf -- '{}' '+'
